import {formatTime} from "../../utils/util";
import {formatDuration, formatFee} from "../../utils/format";

const initialLat = 30
const initialLng = 120

function  formatD(sec : number){
    const dur = formatDuration(sec)
    return `${dur.hh}:${dur.mm}:${dur.ss}`
}


Page({
    timer: undefined as number |undefined,
    data: {
        location: {
            latitude: initialLat,
            longitude: initialLng,
        },
        scale: 12,
        elapsed: '00:00:00',
        fee: '0.00',
        markers: [
            {
                iconPath: "/resources/car.png",
                id: 0,
                latitude: initialLat,
                longitude: initialLng,
                width: 20,
                height: 20,
            },
        ],


    },
    onLoad: function (options) {
        this.setupLocationUpdator(),
            this.setupTimer()
    },
    onUnload(){
        wx.stopLocationUpdate();
        if(this.timer){
            clearInterval(this.timer)
        }
    },

    setupLocationUpdator() {
        wx.startLocationUpdate({
            fail: console.error,
        })
        wx.onLocationChange(loc => {
            console.log(loc)
            this.setData({
                location: {
                    latitude: loc.latitude,
                    longitude: loc.longitude,
                },
            })
        })
    },

    onEndTripTap(){


    },


    async setupTimer(tripID: string) {

        let secSinceLastUpdate = 0
        let cents=0
       this.timer= setInterval(
            ()=>{
                secSinceLastUpdate++;
                cents=cents+secSinceLastUpdate;
                this.setData({
                    elapsed: formatD(secSinceLastUpdate),
                    fee: formatFee(cents),
                })
            },1000
        )

    },

});